﻿using AutoMapper;
using MediatR;
using System.IO;
using ZGLXM.Domain;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Infra.Repository;
using ZGLXM.Writh.Api.Applicantion.Command.RBAC;

namespace ZGLXM.Writh.Api.Applicantion.Handler.RBAC
{
    public class RoleInsertCommandHandler : IRequestHandler<RoleInsertCommand, ApiResult>
    {
        private readonly IRoleRepository roleRepository;
        private readonly IMapper mapper;

        public RoleInsertCommandHandler(IRoleRepository roleRepository, IMapper mapper)
        {
            this.roleRepository = roleRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> Handle(RoleInsertCommand request, CancellationToken cancellationToken)
        {
            var role = mapper.Map<Role>(request);
            var res = await roleRepository.InsertAsync(role);
            if (res > 0)
            {
                return ApiResult.OK("新增角色成功");
            }
            else
            {
                return ApiResult.Fail("新增角色失败");
            }
        }
    }
}
